package com.zpp.wedding_admin.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zpp.wedding_admin.entity.CameraWork;
import com.zpp.wedding_admin.entity.Collect;
import com.zpp.wedding_admin.entity.Good;
import com.zpp.wedding_admin.entity.User;
import com.zpp.wedding_admin.service.ICameraWorkService;
import com.zpp.wedding_admin.service.ICollectService;
import com.zpp.wedding_admin.service.IGoodService;
import com.zpp.wedding_admin.service.IUserService;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.*;

@Api(description = "前台帖子操作")
@CrossOrigin
@RestController
@RequestMapping("/weddingindex/work")
public class ApiCameraWorkController {

    @Autowired
    private ICameraWorkService workService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ICollectService collectService;

    @Autowired
    private IGoodService goodService;

    @ApiOperation(value = "获取所有帖子")
    @GetMapping("getAll")
    public R getAllWork() {
        List<CameraWork> list = workService.getAllList();
        return R.ok().data("workList", list);
    }

    @ApiOperation(value = "获取用户所有帖子")
    @GetMapping("getAllListByUserId/{id}")
    public R getAllListByUserId(@PathVariable Integer id) {
        List<CameraWork> workList = workService.getAllListByUserId(id);
        return R.ok().data("workList", workList);
    }

    @ApiOperation(value = "通过帖子id获取单个帖子")
    @GetMapping("getByWorkId/{id}")
    public R getByWorkId(@PathVariable Integer id) {
        CameraWork work = workService.getByWorkId(id);
        return R.ok().data("work", work);
    }

    @ApiOperation(value = "点赞帖子")
    @GetMapping("goodAdd/{id}/{userId}")
    public R goodAdd(@PathVariable String id,
                     @PathVariable String userId) {
        boolean c = userService.goodAdd(userId);
        boolean b = workService.goodAdd(id);
        boolean a = goodService.goodAdd(id, userId);
        if (a && b && c) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "取消点赞帖子")
    @GetMapping("goodSubtract/{id}/{userId}")
    public R goodSubtract(@PathVariable String id,
                          @PathVariable String userId) {
        boolean c = userService.goodSubtract(userId);
        boolean b = workService.goodSubtract(id);
        boolean a = goodService.goodSubtract(id, userId);
        if (a && b && c) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "收藏帖子")
    @GetMapping("collectAdd/{id}/{userId}")
    public R badAdd(@PathVariable String id, @PathVariable String userId) {
        boolean c = userService.collectAdd(userId);
        boolean b = workService.collectAdd(id);
        boolean a = collectService.collectAdd(id, userId);
        if (a && b && c) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "取消收藏帖子")
    @GetMapping("collectSubtract/{id}/{userId}")
    public R badSubtract(@PathVariable String id, @PathVariable String userId) {
        boolean c = userService.collectSubtract(userId);
        boolean b = workService.collectSubtract(id);
        boolean a = collectService.collectSubtract(id, userId);
        if (a && b && c) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("getWorkPageVo/{currentPage}/{size}/{id}/{userId}")
    public R getBannerPageVo(@PathVariable Integer currentPage,
                             @PathVariable Integer size,
                             @PathVariable Integer id,
                             @PathVariable Integer userId,
                             @RequestBody Integer[][] allList) {
        Integer labelIDs[] = allList[0];
        Integer locationIDs[] = allList[1];
        Map<String, Object> map = getList(id, labelIDs, locationIDs, currentPage, size, userId);
        List<CameraWork> workList = (List<CameraWork>) map.get("list");
        Integer total = (Integer) map.get("total");
        Boolean[] collectIds = (Boolean[]) map.get("collectIds");
        Boolean[] goodIds = (Boolean[]) map.get("goodIds");
        return R.ok().
                data("workList", workList).
                data("total", total).
                data("goodIds", goodIds).
                data("collectIds", collectIds);
    }

    public Map<String, Object> getList(Integer id, Integer labelIDs[], Integer locationIDs[], Integer current, Integer limit, Integer userId) {
        Map<String, Object> data = new HashMap<>();
        Map<Integer, CameraWork> map = new HashMap<>();
        //通过传递的条件获取当前条件下的帖子
        List<CameraWork> list = workService.getWorkPageVo(id, labelIDs, locationIDs);
        //获取总数，相当于分页里面的total
        data.put("total", list.size());
        //将符合条件的所有帖子放到哈希表中，以id为key
        for (CameraWork cameraWork : list) {
            map.put(cameraWork.getWorkId(), cameraWork);
        }
        //存储帖子并进行排序
        List<CameraWork> cameraWorkList = new ArrayList<>(map.size());
        map.forEach((key, value) -> {
            cameraWorkList.add(value);
        });
        cameraWorkList.sort((o1, o2) -> o2.getWorkId() - o1.getWorkId());
        int size = 0;
        List<CameraWork> newList = new ArrayList<>();
        for (int i = (current - 1) * limit; i < current * limit && i < cameraWorkList.size(); i++) {
            newList.add(cameraWorkList.get(i));
        }
        //
        List<CameraWork> allCollect = getAllCollect(userId.toString());
        Map<Integer, CameraWork> cameraWorkMap = new HashMap<>(allCollect.size());
        allCollect.forEach(work -> cameraWorkMap.put(work.getWorkId(), work));
        Boolean[] collectIds = new Boolean[limit];
        for (int i = 0; i < newList.size(); i++) {
            if ( cameraWorkMap.containsKey(newList.get(i).getWorkId()) ) {
                collectIds[i] = true;
            } else {
                collectIds[i] = false;
            }
        }
        //
        cameraWorkMap.clear();
        //
        List<CameraWork> allGood = getAllGood(userId.toString());
        allGood.forEach(work -> cameraWorkMap.put(work.getWorkId(), work));
        Boolean[] goodIds = new Boolean[limit];
        for (int i = 0; i < newList.size(); i++) {
            if ( cameraWorkMap.containsKey(newList.get(i).getWorkId()) ) {
                goodIds[i] = true;
            } else {
                goodIds[i] = false;
            }
        }
        data.put("list", newList);
        data.put("collectIds", collectIds);
        data.put("goodIds", goodIds);
        return data;
    }

    public List<CameraWork> getAllCollect(String id) {
        List<Collect> collectList = collectService.getCollect(id);
        List<CameraWork> cameraWorkList = new ArrayList<>();
        collectList.forEach(collect -> cameraWorkList.add(collect.getCameraWork()));
        return cameraWorkList;
    }

    public List<CameraWork> getAllGood(String id) {
        List<Good> goodList = goodService.getGood(id);
        List<CameraWork> cameraWorkList = new ArrayList<>();
        goodList.forEach(collect -> cameraWorkList.add(collect.getCameraWork()));
        return cameraWorkList;
    }
}
